package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.FinanceManagement;
import com.ruoyi.recycle.domain.HomePageSlideshow;
import com.ruoyi.recycle.dto.HomePageSlideshowDTO;
import com.ruoyi.recycle.service.IHomePageSlideshowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 首页轮播广告图片Controller
 *
 * @author Wubk
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/slideshow")
@Api(tags = "首页轮播广告图片")
public class HomePageSlideshowController extends BaseController {
    @Autowired
    private IHomePageSlideshowService homePageSlideshowService;

    /**
     * 查询首页轮播广告图片列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HomePageSlideshow homePageSlideshow) {
        startPage();
        List<HomePageSlideshow> list = homePageSlideshowService.selectHomePageSlideshowList(homePageSlideshow);
        return getDataTable(list);
    }

    /**
     * 分页条件查询
     */
    @PostMapping("/slideshowPageQuery")
    @ApiOperation("分页条件查询")
    public IPage<HomePageSlideshow> slideshowPageQuery(@RequestBody HomePageSlideshowDTO queryDTO) throws Exception{
        final HomePageSlideshow slideshow = new HomePageSlideshow();
        BeanUtils.copyProperties(queryDTO, slideshow);
        final LambdaQueryWrapper<HomePageSlideshow> wrapper = Wrappers.lambdaQuery(slideshow);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(HomePageSlideshow::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(HomePageSlideshow::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));
        }
        wrapper.orderByDesc(HomePageSlideshow::getCreatedTime);
        return homePageSlideshowService.page(new Page<HomePageSlideshow>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 导出首页轮播广告图片列表
     */
    @RequiresPermissions("recycle:slideshow:export")
    @Log(title = "首页轮播广告图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出首页轮播广告图片列表")
    public void export(HttpServletResponse response, HomePageSlideshow homePageSlideshow) {
        List<HomePageSlideshow> list = homePageSlideshowService.selectHomePageSlideshowList(homePageSlideshow);
        ExcelUtil<HomePageSlideshow> util = new ExcelUtil<HomePageSlideshow>(HomePageSlideshow.class);
        util.exportExcel(response, list, "首页轮播广告图片数据");
    }

    /**
     * 获取首页轮播广告图片详细信息
     */
    @RequiresPermissions("recycle:slideshow:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取首页轮播广告图片详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(homePageSlideshowService.selectHomePageSlideshowById(id));
    }

    /**
     * 新增首页轮播广告图片
     */
    @RequiresPermissions("recycle:slideshow:add")
    @Log(title = "首页轮播广告图片", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增首页轮播广告图片")
    public AjaxResult add(@RequestBody HomePageSlideshow homePageSlideshow) {
        homePageSlideshow.setCreatedTime(new Date());
        return toAjax(homePageSlideshowService.insertHomePageSlideshow(homePageSlideshow));
    }

    /**
     * 修改首页轮播广告图片
     */
    @RequiresPermissions("recycle:slideshow:edit")
    @Log(title = "首页轮播广告图片", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改首页轮播广告图片")
    public AjaxResult edit(@RequestBody HomePageSlideshow homePageSlideshow) {
        homePageSlideshow.setUpdatedTime(new Date());
        return toAjax(homePageSlideshowService.updateHomePageSlideshow(homePageSlideshow));
    }

    /**
     * 删除首页轮播广告图片
     */
    @RequiresPermissions("recycle:slideshow:remove")
    @Log(title = "首页轮播广告图片", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除首页轮播广告图片")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(homePageSlideshowService.deleteHomePageSlideshowByIds(ids));
    }
}
